package structure;

/**
 * Created by yanbin on 2017/8/16 0016.
 * 栈的数组实现
 */
public class ArrayStack implements Stack<String>{

    private String[] list;  //栈内容

    private int maxSize;    //栈的容量

    private int top;        //栈顶位置

    public ArrayStack() {}

    /**
     * 初始化栈
     * @param maxSize 容量
     */
    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        list = new String[maxSize];
        top = -1;
    }

    public String[] getList() {
        return list;
    }

    public void setList(String[] list) {
        this.list = list;
    }

    public int getMaxSize() {
        return maxSize;
    }

    public void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
    }

    public int getTop() {
        return top;
    }

    public void setTop(int top) {
        this.top = top;
    }

    public void push(String element) {
        list[++top] = element;
    }

    public String pop() {
        return list[top--];
    }

    public String peek() {
        return list[top];
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public int size() {
        return top + 1;
    }

    public String displayStack() {
        StringBuffer sb = new StringBuffer();
        sb.append("Stack (bottom --> top):");
        for (String item : list) {
            sb.append(item).append(" ");
        }
        return sb.toString();
    }


}
